package com.noodles.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.noodles.mall.entity.Cart;
import com.noodles.mall.mapper.CartMapper;
import com.noodles.mall.service.CartService;
import com.noodles.mall.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    ItemService itemService;

    /** 增加 **/
    public boolean insert(Cart cart){
        return super.save(cart);
    }

    /** 删除 **/
    public boolean delete(List<Integer> ids){
        return super.removeByIds(ids);
    }

    /** 修改: 只修改商品数量**/
    public boolean update(Cart cart){
        return super.update(new UpdateWrapper<Cart>().lambda()
                .set(Cart::getItemCount,cart.getItemCount())
                .eq(Cart::getId,cart.getId())
        );
    }

    /** 查询: 单个 **/
    public Cart selectOne(Integer userId,Integer itemId){
        return super.getOne(new QueryWrapper<Cart>().lambda()
                .eq(Cart::getUserId,userId)
                .eq(Cart::getItemId,itemId)
        );
    }

    /** 查询: 商品数量 **/
    public Integer selectCount(Integer userId){
        return super.count(new QueryWrapper<Cart>().lambda()
                .eq(Cart::getUserId,userId)
        );
    }

    /** 分页查询: 根据用户id **/
    public List<Cart> selectList(Integer userId){
        List<Cart> list=super.list(new QueryWrapper<Cart>().lambda()
                .eq(Cart::getUserId,userId));
        list.forEach(cart -> cart.setItem(itemService.selectOne(cart.getItemId())));
        return list;
    }
}
